package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/checkgroup")
public class CheckGroupController {
    @Reference
    private CheckGroupService checkGroupService;
    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return checkGroupService.find(queryPageBean);
    }
    @GetMapping("/findById")
    public Result findById(@RequestParam("id") Integer id){
        try {
            CheckGroup checkGroup = checkGroupService.findById(id);
            Result result =new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS);
            result.setData(checkGroup);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    @PostMapping("/edit")
    public Result update(@RequestParam("checkitemIds") ArrayList<String> checkitemIds, @RequestBody CheckGroup checkGroup){
        try {
            ArrayList<Integer> list = new ArrayList<>();
            for (String s : checkitemIds) {
                int i = Integer.parseInt(s);
                list.add(i);
            }
            Boolean update = checkGroupService.update(list, checkGroup);
            if (update){
                return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false,MessageConstant.EDIT_CHECKITEM_FAIL);
    }
    @GetMapping("/findCheckItemIdsByCheckGroupId")
    public Result findByIds(@RequestParam("id") Integer id){
        try {
            ArrayList<Integer> ids = checkGroupService.findIdsBygroupId(id);
            if (ids.size()!=0){
                Result result =new Result(true,"根据监察局id查询检查项成功");
                result.setData(ids);
                return result;
            }

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"根据监察局id查询检查项失败");
        }
        return new Result(false,"根据监察局id查询检查项失败");
    }

    @PostMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup ,@RequestParam("checkitemIds") List<Integer> checkitemIds){
        if (checkGroup.getCode() == null && checkGroup.getName() == null) {
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        Integer count= this.checkGroupService.add(checkGroup,checkitemIds);
        if (count == 1) {
            return new Result(true,  MessageConstant.ADD_CHECKGROUP_SUCCESS);
        }
        return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
    }
    /**
     * 删除组
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public Result deleteGroup(@RequestParam Integer id){
        if (null == id){
            return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
        try {
            Integer delete = checkGroupService.delete(id);
            if (delete == 1) {
                return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
        return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
    }
}
